	.text
	.align 2
_allocRecord_:
	addi $a1, $zero, 0
_initArray_:
	add $t0, $zero, $a0
	sll $a0, $a0, 2
	li $v0, 9
	syscall
	add $a0, $a0, $v0
initArray_loop:
	beq $t0, $zero, initArray_done
	sll $t1, $t0, 2
	sub $t1, $a0, $t1
	sw $a1, 0($t1)
	addi $t0, $t0, -1
	j initArray_loop
initArray_done:
	jr $ra

	.text
	.align 2
_print_:
	addi $a0, $a0, 4
	li $v0, 4
	syscall
	jr $ra

	.text
	.align 2
_ord_:
	addi $v0, $zero, -1
	lw $t0, 0($a0)
	beq $t0, $zero, ord_done
	lb $v0, 4($a0)
ord_done:
	jr $ra

	.data
	.align 2
chr_left_paren:
	.asciiz "chr("
out_of_range:
	.asciiz ") out of range\n"

	.text
	.align 2
_chr_:
	add $t0, $zero, $a0
	srl $t0, $t0, 8
	bne $t0, $zero, chr_exception
	la $v0, consts
	sll $t0, $a0, 3
	add $v0, $v0, $t0 # $v0 <- consts + i * 8
	jr $ra
chr_exception:
	add $t0, $zero, $a0 # store the parameter i
	la $a0, chr_left_paren
	li $v0, 4
	syscall # print "chr("
	add $a0, $zero, $t0
	li $v0, 1
	syscall # print the parameter i
	la $a0, out_of_range
	li $v0, 4
	syscall # print ") out of range \n"
	li $a0, 1
	li $v0, 17
	syscall # exit(1)

	.text
	.align 2
_size_:
	lw $v0, 0($a0)
	jr $ra

	.data
	.align 2
flush_tip:
	.asciiz "No Flush Man!\n"

	.text
	.align 2
_flush_:
	la $a0, flush_tip
	li $v0, 4
	syscall
	jr $ra

	.data
	.align 2
substring_paren_brack:
	.asciiz "substring(["
right_brack_comma:
	.asciiz "],"
comma:
	.asciiz ","

	.text
	.align 2
_substring_:
	or $t0, $a1, $a2
	slt $t0, $t0, $zero
	bne $t0, $zero, substring_exception # first < 0 || n < 0
	add $t0, $a1, $a2 # $t0 <- first + n
	lw $t1, 0($a0) # $t1 <- s->length
	slt $t0, $t1, $t0
	bne $t0, $zero, substring_exception # first + n > s->length
	beq $a2, $zero, substring_empty # n == 0
	addi $t0, $a2, -1
	beq $t0, $zero, substring_single # n == 1
	addi $t0, $a0, 4 # $t0 <- &(s->chars[0])
	add $t0, $t0, $a1 # $t0 <-&(s->chars[first])
	addi $a0, $a2, 4 # $a0 <- n + 4
	li $v0, 9
	syscall # t <- malloc(n + 4), $v0 <- t
	sw $a2, 0($v0) # t->length <- n
	addi $t1, $v0, 4 # $t1 <- &(t->chars[0])	
	li $t2, 0 # $t2 loop variable	
substring_loop:
	beq $t2, $a2, substring_done
	lb $t3, 0($t0)
	sb $t3, 0($t1)
	addi $t0, $t0, 1
	addi $t1, $t1, 1
	addi $t2, $t2, 1
	j substring_loop
substring_empty:
	la $v0, empty
substring_done:
	jr $ra
substring_single:
	add $t0, $a0, $a1
	lb $t0, 4($t0) # $t0 < s->chars[first]
	la $v0, consts
	sll $t0, $t0, 3
	add $v0, $v0, $t0
	jr $ra
substring_exception:
	lw $t0, 0($a0) # $t0 <- s->length
	la $a0, substring_paren_brack
	li $v0, 4
	syscall # print("substring([")
	add $a0, $zero, $t0
	li $v0, 1
	syscall # print("%d", s->length)
	la $a0, right_brack_comma
	li $v0, 4
	syscall # print("],")
	add $a0, $zero, $a1
	li $v0, 1
	syscall # print("%d", first)
	la $a0, comma
	li $v0, 4
	syscall # print(",")
	add $a0, $zero, $a2
	li $v0, 1
	syscall # print("%d", n)
	la $a0, out_of_range
	li $v0, 4
	syscall # print(") out of range\n")
	li $a0, 1
	li $v0, 17
	syscall # exit(1)

	.text
	.align 2
_concat_:
	lw $t0, 0($a0) # $t0 <- a->length
	beq $t0, $zero, ret_b
	lw $t1, 0($a1) # $t1 < b->length
	beq $t1, $zero, ret_a
	add $t2, $t0, $t1 #	$t2 <- a->length + b->length
	addi $t3, $a0, 4 # $t3 <- &(a->chars[0])
	addi $a0, $t2, 4
	li $v0, 9
	syscall
	sw $t2, 0($v0) # t->length <- a->length + b->length
	addi $t2, $v0, 4 # $t2 < &(t->chars[0])
	li $t4, 0
a_copy_loop:
	beq $t4, $t0, a_copy_done
	lb $t5, 0($t3)
	sb $t5, 0($t2)
	addi $t2, $t2, 1
	addi $t3, $t3, 1
	addi $t4, $t4, 1
	j	a_copy_loop
a_copy_done:
	add $t3, $a1, 4 # $t3 <- &(b->chars[0])
	li $t4, 0
b_copy_loop:
	beq $t4, $t1, b_copy_done
	lb $t5, 0($t3)
	sb $t5, 0($t2)
	addi $t2, $t2, 1
	addi $t3, $t3, 1
	addi $t4, $t4, 1
	j b_copy_loop
ret_a:
	add $v0, $a0, $zero
b_copy_done:
	jr $ra
ret_b:
	add $v0, $a1, $zero
	jr $ra	

	.text
	.align 2
_not_:
	li $v0, 0
	bne $a0, $zero, not_done
	li $v0, 1
not_done:
	jr $ra

	.text 
	.align 2
_getchar_:
	li $v0, 12
	syscall
	beq $v0, $zero, getchar_empty
	sll $t0, $v0, 3
	la $v0, consts
	add $v0, $v0, $t0
	jr $ra
getchar_empty:
	la $v0, empty
	jr $ra

	.text
	.align 2
_stringEqual_:
	li $v0, 1
	beq $a0, $a1, str_equ		
	lw $t0, 0($a0)
	lw $t1, 0($a1)
	bne $t0, $t1, str_neq
	addi $t1, $a0, 4 # $t1 <- &(s->chars[0])
	addi $t2, $a1, 4 # $t2 <- &(t->chars[0])
	li $t3, 0 # loop variable
stringEqual_loop:
	beq $t3, $t0, str_equ
	lb $t4, 0($t1)
	lb $t5, 0($t2)
	bne $t4, $t5, str_neq
	addi $t1, $t1, 1
	addi $t2, $t2, 1
	addi $t3, $t3, 1
	j	stringEqual_loop
str_equ:
	li $v0, 0
str_neq:
	jr $ra

	.text
	.align 2
_exit_:
	li $v0, 17
	syscall
	jr $ra

	.data
	.align 2
consts:
	.space 2048
	
	.data
	.align 2
empty:
	.word 0
	.asciiz ""

	.text
	.align 2
main:
	addi $sp, $sp, -16
	sw $ra, 12($sp)
	la $t0, consts
	li $t1, 256
	li $t2, 0
	li $t3, 1
consts_loop:
	beq $t2, $t1, consts_done
	sw $t3, 0($t0) # consts[i].length <- 1
	sw $t2, 4($t0) # consts[i].chars[0] <- i
	addi $t0, $t0, 8
	addi $t2, $t2, 1
	j consts_loop
consts_done:
	jal tigermain
	lw $ra, 12($sp)
	addi $sp, $sp, 16
	jr $ra

